package com.chinawlx.wlxteacher.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;

import com.chinawlx.wlxteacher.base.WLXApplication;
import com.chinawlx.wlxteacher.network.bean.WLXGetCodeBean;
import com.google.gson.Gson;

import java.net.HttpURLConnection;
import java.net.URL;

import okhttp3.ResponseBody;

/**
 * 作者:吴名
 * 时间:16/8/4
 * 描述:联网相关工具类
 */
public class WLXNetUtil {
    /**
     * 判断网络连接是否打开
     */
    public static boolean CheckNetwork(Context ctx) {
        boolean flag = false;
        ConnectivityManager netManager = (ConnectivityManager) ctx.getSystemService(Context
                .CONNECTIVITY_SERVICE);
        if (netManager.getActiveNetworkInfo() != null) {
            flag = netManager.getActiveNetworkInfo().isAvailable();
        }
        return flag;
    }

    public static boolean netIsAvailable() {
        boolean flag = false;
        ConnectivityManager netManager = (ConnectivityManager) WLXApplication.myApplication
                .getSystemService(Context
                .CONNECTIVITY_SERVICE);
        if (netManager.getActiveNetworkInfo() != null) {
            flag = netManager.getActiveNetworkInfo().isAvailable();
        }
        return flag;
    }


    /**
     * 判断是否是wifi连接
     */
    public static boolean isWifi(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context
                .CONNECTIVITY_SERVICE);

        return cm != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;

    }

    /**
     * 打开网络设置界面
     */
    public static void openSetting(Activity activity) {
        Intent intent = new Intent("/");
        ComponentName cm = new ComponentName("com.android.settings", "com.android.settings" + "" +
                ".WirelessSettings");
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        activity.startActivityForResult(intent, 0);
    }

    /**
     * 检查url是否有效
     */
    public static boolean checkURL(String url) {
        boolean value = false;
        try {
            HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
            int code = conn.getResponseCode();
            if (code != 200) {
                value = false;
            } else {
                value = true;
            }
        } catch (Exception e) {
        }
        return value;
    }

    /**
     * 检查请求结果是否成功
     *
     * @param responseBody
     * @return
     */
    public static boolean checkSuccess(ResponseBody responseBody) {
        String json = getResponsString(responseBody);
        WLXLogUtil.d(json);
        Gson gson = new Gson();
        WLXGetCodeBean wlxGetCodeBean = gson.fromJson(json, WLXGetCodeBean.class);
        if (wlxGetCodeBean.getCode() == 0) {
            return true;
        }
        return false;
    }

    public static String getResponsString(ResponseBody body) {
        try {
            byte[] bytes = body.bytes();
            return new String(bytes, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
